#include "n32g45x.h"

#include "bsp_tim.h"



void TIM1_Init(void)
{	
	TIM_TimeBaseInitType TIM_TimeBaseStructure;
	OCInitType TIM_OCInitStructure;
	uint16_t PrescalerValue = 0;
	
	////////////////RCCʱ������////////////////
	/* PCLK2 = HCLK/4 */
    RCC_ConfigPclk2(RCC_HCLK_DIV4);//����PCLKƵ��ΪHCLK/4,HCLKĬ��Ϊ144MHz
    /* TIM1 clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
	
	////////////////NVIC�ж�ͨ������////////////////
	NVIC_InitType NVIC_InitStructure;
    /* Enable the TIM1 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
	
	////////////////TIM1�߼���ʱ������////////////////
	/* Compute the prescaler value */
	//������������ô�ʱ������Ƶ��Ϊ36MHz������Ҫ1ms��һ����������Ҫ��Ƶ����Ƶϵ��ΪPrescalerValue=PCLK2/dT_ms-1
    PrescalerValue = 36000 - 1; //(uint16_t) (SystemCoreClock / 12000000) - 1;
    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.Period    = 1000;//����������
    TIM_TimeBaseStructure.Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);
    /* Prescaler configuration */
    TIM_ConfigPrescaler(TIM1, PrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);
    /* TIM1 enable update irq */
    TIM_ConfigInt(TIM1, TIM_INT_UPDATE, ENABLE);
    /* TIM1 enable counter */
    TIM_Enable(TIM1, ENABLE);	
}

void TIM6_Init(void)
{
	TIM_TimeBaseInitType TIM_TimeBaseStructure;
	OCInitType TIM_OCInitStructure;
	uint16_t PrescalerValue = 0;
	
	/* PCLK1 = HCLK/4 */
    RCC_ConfigPclk1(RCC_HCLK_DIV4);//����PCLKƵ��ΪHCLK/4,HCLKĬ��Ϊ144MHz
    /* TIM6 clock enable */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM6, ENABLE);
	
	/* Compute the prescaler value */
    PrescalerValue = 36000-1; //(uint16_t) (SystemCoreClock / 12000000) - 1;
    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.Period    = 1000;
    TIM_TimeBaseStructure.Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM6, &TIM_TimeBaseStructure);
    /* Prescaler configuration */
    TIM_ConfigPrescaler(TIM6, PrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);
    /* TIM6 enable update irq */
    TIM_ConfigInt(TIM6, TIM_INT_UPDATE, ENABLE);
    /* TIM6 enable counter */
    TIM_Enable(TIM6, ENABLE);
	
	NVIC_InitType NVIC_InitStructure;
    /* Enable the TIM6 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM6_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void TIM7_Init(void)
{
	
}

void TIM_Init(void)
{
	TIM1_Init();//�߼����ƶ�ʱ��1��ʼ��
	TIM6_Init();//������ʱ��2��ʼ��
}

void TIM1_IRQ(void)
{
	Drv_LED_Toggle(LED_3);
}

void TIM6_IRQ(void)
{
    Drv_LED_Toggle(LED_1);
}



